package com.tech.koufu.ui.activity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tech.koufu.MainActivity;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.action.StockManager;
import com.tech.koufu.model.UserGroups;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.tools.SharedConfig;
import com.tech.koufu.tools.Statics;
import com.tech.koufu.ui.activity.login.BaseThridLoginActivity;
import com.tech.koufu.ui.activity.login.LoginActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Toast;

public class WelcomeActivity extends BaseThridLoginActivity{
	private static final String TAG = WelcomeActivity.class.getName();
	private boolean first;	//判断是否第一次打开软件
	private View view;
	private Context context;
	private Animation animation;
	private SharedPreferences shared;
	private StringRequest mRequest;
	private static int TIME = 1000; 		
	MyApplication myApp;
//	Handler handler = new Handler(); 
	private RequestQueue mQueue;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		view = View.inflate(this, R.layout.activity_welcome, null);
		setContentView(view);
		context = this;							//得到上下文
		shared = new SharedConfig(context).GetConfig(); 	// 得到配置文件 
		ShareSDK.initSDK(this);  
		autoLogin();
//		handler.postDelayed(new Runnable() {
//
//			@Override
//			public void run() {
//				Intent intent = new Intent(getApplicationContext(),MainActivity.class);
//				startActivity(intent);
//				finish();
//			}
//		}, 2000);
//

	} 

	@Override
	protected void onResume() {  
		super.onResume();
		into();
	}

	public void onPause() {
		super.onPause();
	}
	// 进入主程序的方法
	private void into() {

		// 如果网络可用则判断是否第一次进入，如果是第一次则进入欢迎界面
		first = shared.getBoolean("First", true);

		// 设置动画效果是alpha，在anim目录下的alpha.xml文件中定义动画效果
		animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
		// 给view设置动画效果
		view.startAnimation(animation);
		animation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation arg0) {
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
			}

			// 这里监听动画结束的动作，在动画结束的时候开启一个线程，这个线程中绑定一个Handler,并
			// 在这个Handler中调用goHome方法，而通过postDelayed方法使这个方法延迟500毫秒执行，达到
			// 达到持续显示第一屏500毫秒的效果
			@Override
			public void onAnimationEnd(Animation arg0) {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						Intent intent;
						//如果第一次，则进入引导页WelcomeActivity
						if (first) {
							intent = new Intent(WelcomeActivity.this,
									FirstAnimActivity.class);
						} else {
							intent = new Intent(WelcomeActivity.this,
									MainActivity.class);
						}
						startActivity(intent);
						// 设置Activity的切换效果
						overridePendingTransition(R.anim.in_from_right,
								R.anim.out_to_left);
						WelcomeActivity.this.finish();
					}
				}, TIME);
			}
		});
	}
	private void autoLogin(){ 
		String[] res = SharedConfig.getLoginStatus(getApplicationContext());
		Log.d(TAG, res+"~~");
		if(res!=null){
			if(res[4].equals("qq")){
				qq = ShareSDK.getPlatform(QQ.NAME); 
				authorize(qq);
			}else{
				toLogin(res[0],res[1]);
			}
			
		}
	}
	
	
	
}
